package com.gkd.controller.regular;

import com.gkd.constant.StatusContant;
import com.gkd.context.BaseContext;
import com.gkd.pojo.dto.PostDTO;
import com.gkd.pojo.dto.PostPageQueryDTO;
import com.gkd.pojo.vo.PostVO;
import com.gkd.result.PageResult;
import com.gkd.result.Result;
import com.gkd.service.PostService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 文章相关接口
 */
@RestController("regularPostController")
@CrossOrigin
@Slf4j
@RequestMapping("/regular/post")
public class PostController {

    @Autowired
    private PostService postService;


    /**
     * 文章分页查询
     * @param postPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(PostPageQueryDTO postPageQueryDTO){
        log.info("文章分页查询:{}",postPageQueryDTO);
        postPageQueryDTO.setStatus(StatusContant.POST_PUBLISH);
        PageResult pageResult=postService.pageQuery(postPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据文章id获取数据
     * @param postId
     * @return
     */
    @GetMapping("/{postId}")
    public Result<PostVO> getById(@PathVariable Integer postId){
        log.info("根据id获取文章:{}",postId);
        PostVO postVO=postService.getById(postId);
        return Result.success(postVO);
    }

    /**
     * 文章上传
     * @param postDTO
     * @return
     */
    @PostMapping
    public Result add(@RequestBody PostDTO postDTO){
        log.info("文章上传:{}",postDTO);
        postService.add(postDTO);
        return Result.success();
    }

    /**
     * 根据用户id获取文章分页查询
     * @param postPageQueryDTO
     * @return
     */
    @GetMapping("/pageByuserId")
    public Result<PageResult> pageByuserId(PostPageQueryDTO postPageQueryDTO){
        postPageQueryDTO.setUserId(BaseContext.getCurrentId());
        log.info("根据用户id获取文章分页查询:{}",postPageQueryDTO);
        PageResult pageResult=postService.pageQuery(postPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改文章
     * @param postDTO
     */
    @PutMapping
    public Result update(@RequestBody PostDTO postDTO){
        log.info("文章修改:{}",postDTO);
        postService.update(postDTO);
        return Result.success();
    }

    /**
     * 删除文章与关联标签
     * @param postIds
     */
    @DeleteMapping
    public Result deleteByIds(@RequestBody List<Integer> postIds){
        log.info("删除文章:{}",postIds);
        postService.deleteByIds(postIds);
        return Result.success();
    }
}
